package com.ce.study.procum;

import org.apache.log4j.Logger;

public class TestSynchronized {
	private static Logger log = Logger.getLogger(TestSynchronized.class);
	private static int total = 1000;
	private int count = 0;
	
	public int getCount() {
		return count;
	}

	public synchronized void synFun() {
		count++;
		System.out.println("synFun:"+count);
	}
	
	public void synBlock() {
		synchronized (this) {
			count++;
			System.out.println("synBlock:"+count);
		}
	}
	
	public void unSynFun() {
		System.out.println("unSynFun");
	}
	
	static class TSynFunThread extends Thread {
		private TestSynchronized syn;
		
		public TSynFunThread(TestSynchronized syn) {
			this.syn = syn;
		}

		public void run() {
			try {
				while (true) {
					syn.synFun();
					if(syn.getCount()>total)
						break;
					//sleep(1000);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
	static class TSynBlockThread extends Thread {
		private TestSynchronized syn;
		
		public TSynBlockThread(TestSynchronized syn) {
			this.syn = syn;
		}

		public void run() {
			try {
				while (true) {
					syn.synBlock();
					if(syn.getCount()>total)
						break;
					//sleep(1000);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static void main(String[] args) {
//		TestSynchronized syn = new TestSynchronized();
//		for (int i = 0; i < 5; i++) {
//			TSynFunThread synFunRunnable = new TSynFunThread(syn);
//			TSynBlockThread synBlockThread = new TSynBlockThread(syn);
//			synFunRunnable.start();
//			synBlockThread.start();	
//		}
		
		String tmp = null;
		try {
			tmp.toString();
		} catch (Exception e) {
			log.error(e);
			log.error(e, e);
		}
	}
}
